import json

import scrapy
from urllib.parse import urljoin


class UsepostSpider(scrapy.Spider):
    name = "usepost"

    # def start_requests(self):
    #     urls = ['https://www.httpbin.org/post']
    #     yield scrapy.FormRequest(url=urls[0], formdata={'key': 'value'}, callback=self.parse1)
    def start_requests(self):
        urls = ['https://www.httpbin.org/post']
        yield scrapy.Request(url=urls[0], method='POST', body=json.dumps({'key': 'value'}),
                             headers={'Count_Type': 'application/json'}, callback=self.parse1)

    def parse1(self, response):
        print(response.json())
        relative_url = '/path/to/resource'

        # 使用 urllib.parse.urljoin 生成完整的 URL
        full_url = urljoin(response.url, relative_url)
        yield scrapy.Request(url=full_url, callback=self.parse2)

    def parse2(self, response):
        print(response.json(), response.url)
